﻿using LitJson;
using MTUnity.Utils;
using Script.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class UI_WPanel : UIUpdateBase
{
    public UI_Temperature Panel_Temperature;
    public UI_Humidity Panel_Humidity;
    public UI_Repertory Panel_Repertory;
    public UI_StockInOrOut Panel_StockInOrOut;

    private FLocation m_fCurrentLocation;
    // Start is called before the first frame update
    void Start()
    {
        GlobalEvent.AddEvent(EventName.CurrentTemperatureUpdate, OnCurrentTemperatureUpdate);
        GlobalEvent.AddEvent(EventName.CurrentHumidityUpdate, OnCurrentHumidityUpdate);
        GlobalEvent.AddEvent(EventName.CurrentRepertoryUpdate, OnCurrentRepertoryUpdate);
        GlobalEvent.AddEvent(EventName.CurrStockInOrOutUpdate, OnCurrentStockInOrOutUpdate);

        GlobalEvent.AddEvent(EventName.RecordHumitureUpdate, OnRecordHumitureUpdate);
        GlobalEvent.AddEvent(EventName.RecordRepertoryUpdate, OnRecordRepertoryUpdate);
        GlobalEvent.AddEvent(EventName.RecordStockInOrOutUpdate, OnRecordStockInOrOutUpdate);

    }

    public void OnCurrentTemperatureUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Current_Temperature,
            JsonFildName.Warehouse.TemperatureRefreshTime,
        };
        JsonData jsonData = DataGather(listGatherKey);

        DataProcessor(jsonData);
    }

    public void OnCurrentHumidityUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Current_Humidity,
            JsonFildName.Warehouse.HumidityRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);


        DataProcessor(gatherData);
    }

    public void OnCurrentRepertoryUpdate(object param)
    {
        

        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Current_Repertory,
            JsonFildName.Warehouse.RepertoryRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnCurrentStockInOrOutUpdate(object param)
    {


        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Current_StockInOrOut,
            JsonFildName.Warehouse.StockInOrOutRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);


        DataProcessor(gatherData);
    }

    public void OnRecordHumitureUpdate(object param)
    {

        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Record_Temperature,
            JsonFildName.Warehouse.Record_Humidity,
            JsonFildName.Warehouse.TemperatureRefreshTime,
            JsonFildName.Warehouse.HumidityRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    

    public void OnRecordRepertoryUpdate(object param)
    {

        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Record_Repertory,
            JsonFildName.Warehouse.RepertoryRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnRecordStockInOrOutUpdate(object param)
    {

        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Record_StockInOrOut,
            JsonFildName.Warehouse.StockInOrOutRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }




    #region Interface
    public override Dictionary<string , IUIUpdateInterface> RegisterProcessorObj()
    {
        return new Dictionary<string, IUIUpdateInterface>
        {
            {JsonFildName.Warehouse.Temperature , Panel_Temperature },
            {JsonFildName.Warehouse.Humidity , Panel_Humidity },
            {JsonFildName.Warehouse.Repertory , Panel_Repertory },
            { JsonFildName.Warehouse.StockInOrOut , Panel_StockInOrOut},
        };
    }
    public override Dictionary<string , GatherDel> RegisterGatherMethod()
    {
        return new Dictionary<string, GatherDel>
        {
            //current
            { JsonFildName.Warehouse.Current_Temperature , GatherCurrentTemperature},
            { JsonFildName.Warehouse.Current_Humidity , GatherCurrentHumidity},
            { JsonFildName.Warehouse.Current_Repertory , GatherCurrentRepertory},
            { JsonFildName.Warehouse.Current_StockInOrOut , GatherCurrentStockInOrOut},
            //record
            { JsonFildName.Warehouse.Record_Temperature , GatherRecordTemperature},
            { JsonFildName.Warehouse.Record_Humidity , GatherRecordHumidity},
            { JsonFildName.Warehouse.Record_Repertory , GatherRecordRepertory},
            { JsonFildName.Warehouse.Record_StockInOrOut , GatherRecordStockInOrOut},
            //refreshtime
            { JsonFildName.Warehouse.TemperatureRefreshTime , GatherTemperatureRefreshTime},
            { JsonFildName.Warehouse.HumidityRefreshTime , GatherHumidityRefreshTime},
            { JsonFildName.Warehouse.RepertoryRefreshTime , GatherRepertoryRefreshTime},
            { JsonFildName.Warehouse.StockInOrOutRefreshTime , GatherStockInOrOutRefreshTime},
        };
    }
    #endregion

    #region UpdateInterface
    

    #endregion

    public void SetData(FLocation location)
    {
        
        m_fCurrentLocation = location;

        List<string> listGatherKey = new List<string>
        {
            JsonFildName.Warehouse.Current_Temperature,
            JsonFildName.Warehouse.Current_Humidity,
            JsonFildName.Warehouse.Current_Repertory,
            JsonFildName.Warehouse.Current_StockInOrOut,
            JsonFildName.Warehouse.Record_Temperature,
            JsonFildName.Warehouse.Record_Humidity,
            JsonFildName.Warehouse.Record_Repertory,
            JsonFildName.Warehouse.Record_StockInOrOut
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

	#region DataGather
	public JsonData GatherCurrentTemperature()
    {
        return new JsonData(FWarehouse.GetInstance().GetCurrentTemperature(m_fCurrentLocation.LocationType));
    }

    public JsonData GatherCurrentHumidity()
    {
        return new JsonData(FWarehouse.GetInstance().GetCurrentHumidity(m_fCurrentLocation.LocationType));
    }

    public JsonData GatherCurrentRepertory()
    {
        int iCurrentRepertoryInBuff, iCurrentRepertoryInStore = 0;
        FWarehouse.GetInstance().GetCurrentRepertory(m_fCurrentLocation.LocationType, out iCurrentRepertoryInBuff, out iCurrentRepertoryInStore);

        JsonData jsonData = null;
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.Warehouse.TotalRepertoryInBuff,
            iCurrentRepertoryInBuff
            );
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.Warehouse.TotalRepertoryInStore,
            iCurrentRepertoryInStore
            );
        return jsonData;
    }

    public JsonData GatherCurrentStockInOrOut()
    {
        int iStockInOrOut = 0;
        double dEfficient = 0;
        FWarehouse.GetInstance().GetCurrentStockInOrOut(m_fCurrentLocation.LocationType, out iStockInOrOut, out dEfficient);

        JsonData jsonData = null;
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.Warehouse.InOrOutCount,
            iStockInOrOut
            );
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.Warehouse.InOrOutEfficient,
            dEfficient
            );
        return jsonData;
    }

    public JsonData GatherRecordTemperature()
    {
        List<FWarehouseHumitureRecord> listNearestData = FWarehouse.GetInstance().GetNearestHumiture(m_fCurrentLocation.LocationType, 12);
        return JsonUtils.GenerateJsonData(listNearestData);
    }

    public JsonData GatherRecordHumidity()
    {
        List<FWarehouseHumitureRecord> listNearestData = FWarehouse.GetInstance().GetNearestHumiture(m_fCurrentLocation.LocationType, 12);
        return JsonUtils.GenerateJsonData(listNearestData);
    }

    public JsonData GatherRecordRepertory()
    {
        List<FWarehouseRepertoryRecord> listNearestData = FWarehouse.GetInstance().GetNearestRepertoryRecord(m_fCurrentLocation.LocationType, 7);
        return JsonUtils.GenerateJsonData(listNearestData);
    }

    public JsonData GatherRecordStockInOrOut()
    {
        List<FWarehouseStockInOrOutRecord> listNearestData = FWarehouse.GetInstance().GetNearestStockInOrOutRecord(m_fCurrentLocation.LocationType, 7);
        return JsonUtils.GenerateJsonData(listNearestData);
    }


    public JsonData GatherTemperatureRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FEnterOrExitData)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.Warehouse.TemperatureRefreshTime));
    }
    public JsonData GatherHumidityRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FEnterOrExitData)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.Warehouse.HumidityRefreshTime));
    }
    public JsonData GatherRepertoryRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FEnterOrExitData)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.Warehouse.RepertoryRefreshTime));
    }

    public JsonData GatherStockInOrOutRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FEnterOrExitData)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.Warehouse.StockInOrOutRefreshTime));
    }

    #endregion

}
